package com.css.fxfzysx.modules.vabStructure.repository.impl;

import com.css.fxfzysx.modules.cztDataMange.entity.CbhdInfoEntity;
import com.css.fxfzysx.modules.cztDataMange.entity.CpvAnalysisEntity;
import com.css.fxfzysx.modules.cztDataMange.entity.CpvInfoEntity;
import com.css.fxfzysx.modules.cztDataMange.service.BacInformationService;
import com.css.fxfzysx.modules.cztDataMange.vo.QueryBacInformationVo;
import com.css.fxfzysx.modules.vabStructure.repository.PsvAnalysisRepository;
import com.css.fxfzysx.util.CreateGson;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.web.PlatformSessionContext;
import com.css.fxfzysx.zcpt.sys.entity.SUser;
import com.css.fxfzysx.zcpt.sys.service.SUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Repository
public class PsvAnalysisRepositoryImpl implements PsvAnalysisRepository {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Autowired
    private SUserService userService;

    @Override
    public List<CpvAnalysisEntity> getByProjectId(String cpviId) {
        return jdbcTemplate.query("select * from czt_probability_vulnerability_analysis where cpvi_id='"+cpviId+"'",
                new BeanPropertyRowMapper<>(CpvAnalysisEntity.class));
    }

    @Override
    public HashMap<String, Object> queryCpvAnalysis(CpvInfoEntity cpvInfoEntity, int page, int size) {
        StringBuilder sql = new StringBuilder("select * from czt_probability_vulnerability_info where del_flag='0'");
        if (cpvInfoEntity.getRole().equals("1")){
            sql.append(" and create_user='"+ PlatformSessionContext.getUserID() +"'");
        }
        if (!PlatformObjectUtils.isEmpty(cpvInfoEntity.getBasicLibrary())){
            sql.append(" and basic_library='"+ cpvInfoEntity.getBasicLibrary() +"'");
        }
        if (!PlatformObjectUtils.isEmpty(cpvInfoEntity.getStartTime())) {
            sql.append(" and create_time >= '" + cpvInfoEntity.getStartTime() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cpvInfoEntity.getEndTime())) {
            String endTime = cpvInfoEntity.getEndTime() + " 23:59:59";
            sql.append(" and create_time <='" + endTime+"'");
        }
        if (!PlatformObjectUtils.isEmpty(cpvInfoEntity.getProvince())) {
            sql.append(" and province ='" + cpvInfoEntity.getProvince() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cpvInfoEntity.getCity())) {
            sql.append(" and city ='" + cpvInfoEntity.getCity() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(cpvInfoEntity.getCounty())) {
            sql.append(" and county ='" + cpvInfoEntity.getCounty() + "'");
        }
        if(!PlatformObjectUtils.isEmpty(cpvInfoEntity.getStructureType0())){
            sql.append(" and structure_type0 ='" + cpvInfoEntity.getStructureType0() + "'");
        }
        if(!PlatformObjectUtils.isEmpty(cpvInfoEntity.getStructureType1())){
            sql.append(" and structure_type1 ='" + cpvInfoEntity.getStructureType1() + "'");
        }
        if(!PlatformObjectUtils.isEmpty(cpvInfoEntity.getPurpose0())){
            sql.append(" and purpose0 ='" + cpvInfoEntity.getPurpose0() + "'");
        }
        if(!PlatformObjectUtils.isEmpty(cpvInfoEntity.getPurpose1())){
            sql.append(" and purpose1 ='" + cpvInfoEntity.getPurpose1() + "'");
        }
        if(!PlatformObjectUtils.isEmpty(cpvInfoEntity.getFortification())){
            sql.append(" and fortification ='" + cpvInfoEntity.getFortification() + "'");
        }
        if(!PlatformObjectUtils.isEmpty(cpvInfoEntity.getYears())){
            sql.append(" and years ='" + cpvInfoEntity.getYears() + "'");
        }
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(CpvInfoEntity.class)).size();
        StringBuilder orderSql = new StringBuilder("order by create_time desc");
        StringBuilder limitSql = new StringBuilder(" limit " + size + " offset " + (page - 1) * size);
        sql.append(orderSql).append(limitSql);
        List<CpvInfoEntity> data = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(CpvInfoEntity.class));
        for (CpvInfoEntity datum : data) {
            if (PlatformObjectUtils.isNotEmpty(datum.getCreateUser())) {
                String user = userService.getSUser(datum.getCreateUser());
                SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
                datum.setUserName(sUser.getUserName());
            }
            if (PlatformObjectUtils.isNotEmpty(datum.getProvince()) ) {
                StringBuilder region = new StringBuilder(datum.getProvince());
                if(PlatformObjectUtils.isNotEmpty(datum.getCity())){
                    if(!datum.getProvince().equals(datum.getCity())){
                        region.append("-").append(datum.getCity());
                    }
                }
                if(PlatformObjectUtils.isNotEmpty(datum.getCounty())){
                    region.append("-").append(datum.getCounty());
                }
                datum.setRegion(region.toString());
            }
            if (PlatformObjectUtils.isNotEmpty(datum.getStructureType0()) ) {
                StringBuilder st = new StringBuilder(datum.getStructureType0());
                if(PlatformObjectUtils.isNotEmpty(datum.getStructureType1())){
                    st.append("-").append(datum.getStructureType1());
                }
                datum.setSt(st.toString());
            }
            if (PlatformObjectUtils.isNotEmpty(datum.getPurpose0()) && PlatformObjectUtils.isNotEmpty(datum.getPurpose1())) {
                StringBuilder use = new StringBuilder(datum.getPurpose0()).append("-").append(datum.getPurpose1());
                datum.setUse(use.toString());
            }
        }
        HashMap<String, Object> resultMap = new HashMap();
        resultMap.put("total", total);
        resultMap.put("rows", data);
        return resultMap;
    }

    @Override
    public void moveBasicLibrary(String dataId,String operate) {
        String sql = "update czt_probability_vulnerability_info set basic_library=? where cpvi_id = ?";
        jdbcTemplate.update(sql,operate,dataId);
    }
}
